bitkeeper revision 1.1159.1.52 (411cbd47vM82ZdOs6j1qiL3UlWzebA)
authormjw@wray-m-3.hpl.hp.com <mjw@wray-m-3.hpl.hp.com>
Fri, 13 Aug 2004 13:08:23 +0000 (13:08 +0000)
committermjw@wray-m-3.hpl.hp.com <mjw@wray-m-3.hpl.hp.com>
Fri, 13 Aug 2004 13:08:23 +0000 (13:08 +0000)
Abstract message dispatch to methods so that more than one major
message type can be handled.

tools/python/xen/xend/server/blkif.py
tools/python/xen/xend/server/console.py
tools/python/xen/xend/server/controller.py
tools/python/xen/xend/server/netif.py

index d6d034f87c6e3fde6e1cacc75e83060bab69ea1f..cc49688ef59417c74a3b19afdc3660bc50c9e407 100755 (executable)
@@ -20,12 +20,9 @@ class BlkifControllerFactory(controller.ControllerFactory):
 
     def __init__(self):
         controller.ControllerFactory.__init__(self)
-
-        self.majorTypes = [ CMSG_BLKIF_BE ]
-
-        self.subTypes = {
-            CMSG_BLKIF_BE_DRIVER_STATUS_CHANGED: self.recv_be_driver_status_changed,
-            }
+        self.addMethod(CMSG_BLKIF_BE,
+                       CMSG_BLKIF_BE_DRIVER_STATUS_CHANGED,
+                       self.recv_be_driver_status_changed)
         self.attached = 1
         self.registerChannel()
 
@@ -262,15 +259,12 @@ class BlkifController(controller.Controller):
     def __init__(self, factory, dom):
         controller.Controller.__init__(self, factory, dom)
         self.devices = {}
-
-        self.majorTypes = [ CMSG_BLKIF_FE ]
-
-        self.subTypes = {
-            CMSG_BLKIF_FE_DRIVER_STATUS_CHANGED:
-                self.recv_fe_driver_status_changed,
-            CMSG_BLKIF_FE_INTERFACE_CONNECT    :
-                self.recv_fe_interface_connect,
-            }
+        self.addMethod(CMSG_BLKIF_FE,
+                       CMSG_BLKIF_FE_DRIVER_STATUS_CHANGED,
+                       self.recv_fe_driver_status_changed)
+        self.addMethod(CMSG_BLKIF_FE,
+                       CMSG_BLKIF_FE_INTERFACE_CONNECT,
+                       self.recv_fe_interface_connect)
         self.attached = 1
         self.evtchn = None
         self.registerChannel()
index 9221600bdb24500c1a6c7a43a1112be12daf3a1b..df0752893857941de714a13857b79c899a849ff6 100755 (executable)
@@ -113,7 +113,7 @@ class ConsoleController(controller.Controller):
 
     def __init__(self, factory, dom, console_port):
         controller.Controller.__init__(self, factory, dom)
-        self.majorTypes = [ CMSG_CONSOLE ]
+        self.addMethod(CMSG_CONSOLE, 0, None)
         self.status = self.STATUS_NEW
         self.addr = None
         self.conn = None
index e40b72cc885aaa16fabb8f369e5bc3e81a2bd2c6..b3795aecedede6a141338799303ff7259d45ee93 100755 (executable)
@@ -9,7 +9,7 @@ from twisted.internet import defer
 import channel
 from messages import msgTypeName, printMsg
 
-DEBUG = 0
+DEBUG = 1
 
 class Responder:
     """Handler for a response to a message with a specified id.
@@ -56,9 +56,7 @@ class CtrlMsgRcvr:
     @ivar dom: the domain we are a control interface for
     @type dom: int
     @ivar majorTypes: major message types we are interested in
-    @type majorTypes: [int]
-    @ivar subTypes: mapping of message subtypes to methods
-    @ivar subTypes: {int:method}
+    @type majorTypes: {int:{int:method}}
     @ivar timeout: timeout (in seconds) for message handlers
     @type timeout: int
     
@@ -72,8 +70,7 @@ class CtrlMsgRcvr:
 
     def __init__(self):
         self.channelFactory = channel.channelFactory()
-        self.majorTypes = [ ]
-        self.subTypes = {}
+        self.majorTypes = {}
         self.dom = None
         self.channel = None
         self.idx = None
@@ -83,6 +80,37 @@ class CtrlMsgRcvr:
     def setTimeout(self, timeout):
         self.timeout = timeout
 
+    def getMethod(self, type, subtype):
+        """Get the method for a type and subtype.
+
+        @param type: major message type
+        @param subtype: minor message type
+        @return: method or None
+        """
+        method = None
+        subtypes = self.majorTypes.get(type)
+        if subtypes:
+            method = subtypes.get(subtype)
+        return method
+
+    def addMethod(self, type, subtype, method):
+        """Add a method to handle a message type and subtype.
+        
+        @param type: major message type
+        @param subtype: minor message type
+        @param method: method
+        """
+        subtypes = self.majorTypes.get(type)
+        if not subtypes:
+            subtypes = {}
+            self.majorTypes[type] = subtypes
+        subtypes[subtype] = method
+
+    def getMajorTypes(self):
+        """Get the list of major message types handled.
+        """
+        return self.majorTypes.keys()
+
     def requestReceived(self, msg, type, subtype):
         """Dispatch a request message to handlers.
         Called by the channel for requests with one of our types.
@@ -97,7 +125,7 @@ class CtrlMsgRcvr:
         if DEBUG:
             print 'requestReceived>',
             printMsg(msg, all=1)
-        method = self.subTypes.get(subtype)
+        method = self.getMethod(type, subtype)
         if method:
             method(msg, 1)
         elif DEBUG:
@@ -125,7 +153,7 @@ class CtrlMsgRcvr:
             printMsg(msg, all=1)
         if self.callResponders(msg):
             return
-        method = self.subTypes.get(subtype)
+        method = self.getMethod(type, subtype)
         if method:
             method(msg, 0)
         elif DEBUG:
@@ -190,7 +218,7 @@ class CtrlMsgRcvr:
         self.channel = self.channelFactory.domChannel(self.dom)
         self.idx = self.channel.getIndex()
         if self.majorTypes:
-            self.channel.registerDevice(self.majorTypes, self)
+            self.channel.registerDevice(self.getMajorTypes(), self)
         
     def deregisterChannel(self):
         """Deregister interest in our major message types with the
index d78979c992bb32db571455dc5dd0f2af11e0b31f..7a33ab8489e562fd3216cc5b3d37495cb78a1d59 100755 (executable)
@@ -25,12 +25,9 @@ class NetifControllerFactory(controller.ControllerFactory):
 
     def __init__(self):
         controller.ControllerFactory.__init__(self)
-
-        self.majorTypes = [ CMSG_NETIF_BE ]
-
-        self.subTypes = {
-            CMSG_NETIF_BE_DRIVER_STATUS_CHANGED: self.recv_be_driver_status_changed,
-            }
+        self.addMethod(CMSG_NETIF_BE,
+                       CMSG_NETIF_BE_DRIVER_STATUS_CHANGED,
+                       self.recv_be_driver_status_changed)
         self.attached = 1
         self.registerChannel()
 
@@ -218,14 +215,12 @@ class NetifController(controller.Controller):
         controller.Controller.__init__(self, factory, dom)
         self.devices = {}
         
-        self.majorTypes = [ CMSG_NETIF_FE ]
-
-        self.subTypes = {
-            CMSG_NETIF_FE_DRIVER_STATUS_CHANGED:
-                self.recv_fe_driver_status_changed,
-            CMSG_NETIF_FE_INTERFACE_CONNECT    :
-                self.recv_fe_interface_connect,
-            }
+        self.addMethod(CMSG_NETIF_FE,
+                       CMSG_NETIF_FE_DRIVER_STATUS_CHANGED,
+                       self.recv_fe_driver_status_changed)
+        self.addMethod(CMSG_NETIF_FE,
+                       CMSG_NETIF_FE_INTERFACE_CONNECT,
+                       self.recv_fe_interface_connect)
         self.registerChannel()
 
     def sxpr(self):